package com.ylkj.wx.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ylkj.wx.constant.WeiXBaseConstant;
import com.ylkj.wx.dto.ValidateFormDto;
import com.ylkj.wx.facade.EntranceMessageFacade;
import com.ylkj.wx.utils.SignUtil;

@Controller
@RequestMapping("/invalidate")
public class InvalidateContrllor {

    private static final Logger LOGGER = LoggerFactory.getLogger(InvalidateContrllor.class);

    @Resource(name = "entranceMessageFacade")
    private EntranceMessageFacade entranceMessageFacade;
    
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public void sayHello(@ModelAttribute("form") ValidateFormDto form, Model model, @RequestBody String value,
             HttpServletRequest request, PrintWriter out) {

        model.addAttribute("", "");
        String token = WeiXBaseConstant.WEIXIN_TOKEN;
        // add test by wangwp
        String testsignature = request.getParameter("signature");// 微信加密签名
        String testtimestamp = request.getParameter("timestamp");// 时间戳
        String testnonce = request.getParameter("nonce");// 随机数
        String testechostr = request.getParameter("echostr");// 随机字符串
        LOGGER.info("testsignature is " + testsignature);
        LOGGER.info("testtimestamp is " + testtimestamp);
        LOGGER.info("testnonce is " + testnonce);
        LOGGER.info("testechostr is " + testechostr);
        System.out.println("sayHello111");
        
        // 确认请求时来自微信服务器
        if (value == null || StringUtils.isBlank(value)) {
            LOGGER.info("ValidateController index start");
            // 微信首次接入处理
           // entranceMessageFacade.firstEntranceHandel(form, value, out);
            try {
                // 请求校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
                if (SignUtil.checkSignature(token, testsignature, testtimestamp, testnonce)) {
                    out.print(testechostr);
                    out.flush();
                    out.close();
                    LOGGER.info("接入成功！");
                }
            } catch (Exception e) {
                LOGGER.error("接入失败,sha1:! signature:" + testsignature, e.getMessage(), e);
            }
            // 用于处理被动消息
        } else {
            long start = System.currentTimeMillis();
            LOGGER.info("" + start);
            LOGGER.info("公众平台开始接受客户消息!");
            out.print("");
            out.flush();
            out.close();
            // 处理用户发给微信平台的消息
            entranceMessageFacade.messageEntranceHandel(form, value, out);
            long end = System.currentTimeMillis();
            LOGGER.info("" + (end - start));
        }
    }

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public void index(@ModelAttribute("form") ValidateFormDto form, Model model, @RequestBody String value, HttpServletRequest request, PrintWriter out) {
        String token = WeiXBaseConstant.WEIXIN_TOKEN;
        // add test by wangwp
        String testsignature = request.getParameter("signature");// 微信加密签名
        String testtimestamp = request.getParameter("timestamp");// 时间戳
        String testnonce = request.getParameter("nonce");// 随机数
        String testechostr = request.getParameter("echostr");// 随机字符串
        LOGGER.info("testsignature is" + testsignature);
        LOGGER.info("testtimestamp is" + testtimestamp);
        LOGGER.info("testnonce is" + testnonce);
        LOGGER.info("testechostr is" + testechostr);
        System.out.println("value================="+value);
        // add test by wangwp
        // 微信加密签名
    /*    String signature = form.getSignature();
        // 时间戳
        String timestamp = form.getTimestamp();
        // 随机数
        String nonce = form.getNonce();
        // 随机字符串
        String echostr = form.getEchostr();
        LOGGER.info("token is" + token);
        LOGGER.info("signature is" + signature);
        LOGGER.info("timestamp is" + timestamp);
        LOGGER.info("nonce is" + nonce);
        LOGGER.info("echostr is" + echostr);*/
        // 确认请求时来自微信服务器
       /* if (value == null || StringUtils.isBlank(value)) {
            LOGGER.info("ValidateController index start");
            // 微信首次接入处理
            // entranceMessageFacade.firstEntranceHandel(form, value, out);
            try {
                // 请求校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
                if (SignUtil.checkSignature(token, testsignature, testtimestamp, testnonce)) {
                    out.print(testechostr);
                    out.flush();
                    out.close();
                    LOGGER.info("接入成功！");
                }
            } catch (Exception e) {
                LOGGER.error("接入失败,sha1:! signature:" + testsignature, e.getMessage(), e);
            }
            // 用于处理被动消息
        } else {*/
            long start = System.currentTimeMillis();
            LOGGER.info("" + start);
            LOGGER.info("公众平台开始接受客户消息!");
            // 处理用户发给微信平台的消息
            // 处理用户发给微信平台的消息
            entranceMessageFacade.messageEntranceHandel(form, value, out);
            long end = System.currentTimeMillis();
            LOGGER.info("" + (end - start));
      //  }
    }
}
